﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Repository;
using FRL.DebtChased.WorkflowObjects;
using FRL.DebtChased.Silverlight.Library;

namespace FRL.DebtChased.WorkflowEditor.WorkflowItemEditors
{

	/// <summary>
	/// Declaration of PhoneCallCtrl class
	/// Controls for editing the workflow action to call debtor, client, agent or solicitor
	/// </summary>
	public partial class PhoneCallCtrl : ActionBaseCtrl
	{

		/////////////////////////////////////////////////////////////
		// Construction

		public PhoneCallCtrl()
		{

			// Initialize resources
			InitializeComponent();

			// Set drop-down categories
			drpReason.Category = Definitions.ReusableString_PhoneCallReasons;
			drpMessageForCollector.Category = Definitions.ReusableString_PhoneCallMessages;

		}

		/////////////////////////////////////////////////////////////
		// Implementation

		public override List<string> Validate()
		{

			// Declare return variable
			List<string> Ftmp = null;

			// Check controls
			if (drpReason.Text.Length == 0)
			{
				if (Ftmp == null)
					Ftmp = new List<string>();
				Ftmp.Add("Enter a reason to call.");
			}
			if (drpMessageForCollector.Text.Length == 0)
			{
				if (Ftmp == null)
					Ftmp = new List<string>();
				Ftmp.Add("Enter a message for the collector.");
			}

			// Check the days and prioritry
			int iDaySpan = (cboDays.SelectedItem as ComboBoxObject).ItemData;
			WorkflowAction_Base.ePriority priority = (WorkflowAction_Base.ePriority)(cboPriority.SelectedItem as ComboBoxObject).ItemData;
			if ((priority == WorkflowAction_Base.ePriority.Escalating) && (iDaySpan == 1))
			{
				if (Ftmp == null)
					Ftmp = new List<string>();
				Ftmp.Add("Cannot have an escalating priority for an action that spans 1 day.");
			}
			
			// Return variable
			return Ftmp;
			
		}

		public override void UpdateControls( WorkflowAction_Base action )
		{

			// Block update events
			this.BlockUpdateEvents = true;
			{

				// Cast the action
				WorkflowAction_PhoneCall phoneCallAction = action as WorkflowAction_PhoneCall;
#if DEBUG
				Debug.Assert(phoneCallAction != null);
#endif // #if DEBUG

				// Target
				ComboBoxObject.SelectByItemData(cboTarget, (int)phoneCallAction.Target);

				// Set the controls - Priority
				ComboBoxObject.SelectByItemData(cboPriority, (int)phoneCallAction.Priority);

				// Set controls - Reason
				drpReason.Text = phoneCallAction.Reason ?? string.Empty;

				// Set controls - Message
				drpMessageForCollector.Text = phoneCallAction.MessageForCollector ?? string.Empty;

				// Set controls - Additional notes
				txtAdditionalNotes.Text = phoneCallAction.AdditionalNotes ?? string.Empty;

				// Set controls - Days
				ComboBoxObject.SelectByItemData(cboDays, phoneCallAction.DaySpan);

			}
			this.BlockUpdateEvents = false;

			// setup controls
			_setupControls();

		}

		public override void UpdateFromControls(ref WorkflowAction_Base action)
		{

			// If the action needs changing...
			if (!(action is WorkflowAction_PhoneCall))
				action = new WorkflowAction_PhoneCall();

			// Cast the action
			WorkflowAction_PhoneCall phoneCallAction = action as WorkflowAction_PhoneCall;
#if DEBUG
			Debug.Assert(phoneCallAction != null);
#endif // #if DEBUG

			// Update object - Target
			phoneCallAction.Target = (Definitions.eCommTargetType) (cboTarget.SelectedItem as ComboBoxObject).ItemData;

			// Update object - Priority
			phoneCallAction.Priority = (WorkflowAction_Base.ePriority)(cboPriority.SelectedItem as ComboBoxObject).ItemData;

			// Update object - Reason
			phoneCallAction.Reason = drpReason.Text;

			// Update object - Message
			phoneCallAction.MessageForCollector = drpMessageForCollector.Text;

			// Update object - Additional notes
			phoneCallAction.AdditionalNotes = txtAdditionalNotes.Text;

			// Update object = day span
			phoneCallAction.DaySpan = (cboDays.SelectedItem as ComboBoxObject).ItemData;

		}

		public override void InitializeControls()
		{

			// Priority
			WorkflowAction_Base.LoadPriorityDropDown(cboPriority, WorkflowAction_Base.ePriority.Medium);

			// Set controls - Target
			TransportSupport.LoadCommTargetTypeControl(cboTarget, Definitions.eCommTargetType.Debtor);

			// Set controls - Reason
			drpReason.StringList = CachedData.Singleton.PhoneCallReasons;
			drpReason.SetFirstItem();

			// Set controls - Message
			drpMessageForCollector.StringList = CachedData.Singleton.PhoneCallMessages;
			drpMessageForCollector.SetFirstItem();

			// Set controls - Days
			cboDays.Items.Add(new ComboBoxObject("1 day", 1, null));
			cboDays.Items.Add(new ComboBoxObject("2 days", 2, null));
			cboDays.Items.Add(new ComboBoxObject("3 days", 3, null));
			cboDays.Items.Add(new ComboBoxObject("4 days", 4, null));
			cboDays.Items.Add(new ComboBoxObject("5 days", 5, null));
			ComboBoxObject.SelectByItemData(cboDays, 1);

			// unblock update events
#if DEBUG
			Debug.Assert(this.BlockUpdateEvents);
#endif // #if DEBUG
			this.BlockUpdateEvents = false;

			// setup controls
			_setupControls();

		}

		/////////////////////////////////////////////////////////////
		// Event handlers

		private new void ActionBaseCtrl_Loaded(object sender, RoutedEventArgs e)
		{

			// Call base-class function
			base.ActionBaseCtrl_Loaded(sender,e);

		}

		private void drpReason_TextChanged(object sender, RoutedEventArgs e)
		{

			// Shortcuts
			if (this.BlockUpdateEvents)
				return;

			// If this control is in a tabbed panel...
			if(m_pnlAddAction != null)
				m_pnlAddAction.UpdateFromCtrl(this);

		}

		private void cboPriority_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// Shortcuts
			if (this.BlockUpdateEvents)
				return;

			// If this control is in a tabbed panel...
			if (m_pnlAddAction != null)
				m_pnlAddAction.UpdateFromCtrl(this);

		}

		private void cboDays_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// Shortcuts
			if (this.BlockUpdateEvents)
				return;

			// If this control is in a tabbed panel...
			if (m_pnlAddAction != null)
				m_pnlAddAction.UpdateFromCtrl(this);

		}

		private void drpMessageForCollector_TextChange(object sender, RoutedEventArgs e)
		{

			// Shortcuts
			if (this.BlockUpdateEvents)
				return;

			// If this control is in a tabbed panel...
			if (m_pnlAddAction != null)
				m_pnlAddAction.UpdateFromCtrl(this);

		}

		private void txtAdditionalNotes_TextChanged(object sender, TextChangedEventArgs e)
		{

			// Shortcuts
			if (this.BlockUpdateEvents)
				return;

			// If this control is in a tabbed panel...
			if (m_pnlAddAction != null)
				m_pnlAddAction.UpdateFromCtrl(this);

		}

		private void butExpandNotes_Click(object sender, RoutedEventArgs e)
		{

			// Edit notes in big window
			PhoneCallNotesWnd wnd = new PhoneCallNotesWnd(txtAdditionalNotes.Text);
			wnd.Closed += new EventHandler(PhoneCallNotesWnd_Closed);
			wnd.Show();

		}

		void PhoneCallNotesWnd_Closed(object sender, EventArgs e)
		{

			// Get sender
			PhoneCallNotesWnd wnd = sender as PhoneCallNotesWnd;

			// If ok...
			if (wnd.DialogResult ?? false)
			{

				// Update text box
				txtAdditionalNotes.Text = wnd.Message;

			}
			
		}

		/////////////////////////////////////////////////////////////
		// Operations

		private void _setupControls()
		{

			// Enable/disable controls

			// Show/hide controls

			// Show/hide controls- Email Options

		}

	}

}
